(function($, doc) {
	$.init();
	var userinfo = app.getState() || {};
	var uid=userinfo.user_id;
	document.getElementById('user_id').value=uid;
	document.getElementById('mobile').value=userinfo.mobile;
	document.getElementById('a1_money').value=userinfo.a1_money;
	var store_id=0;
	
	$.plusReady(function() {						
		mui.ajax({
			url: apiurl + "Store/apply_store/",
			data: {
				'user_id': userinfo.user_id,
				'_token':userinfo.token
			},
			async: true,
			dataType: 'json',
			crossDomain: true, //强制使用5+跨域
			type: 'get',
			timeout: 10000,
			success: function(res) {
				console.log(JSON.stringify(res));
				if(res.status ==-3){
					mui.toast(res.msg);
					app.out();
					return false;
				}
				// 请求成功
				document.querySelector("#list-area").innerHTML = template('list-template',res);
				if(res.store.store_name && res.store.store_id>0){
					document.getElementById('store_name').value=res.store.store_name;
					document.getElementById('store_name').disabled='disabled';
					var arr_list=document.querySelectorAll('#list-area .mairu_list.store_types .mui-btn');
					mui.each(arr_list,function(index,item){
					  	if(item.getAttribute('data-id')==res.store.type_id){
					  		item.className="mui-btn selected";
					  	}
					});
			
			        var arr_list2=document.querySelectorAll('#list-area .mairu_list.store_business .mui-btn');
					mui.each(arr_list2,function(index,item){
					  	if(item.getAttribute('data-id')==res.store.business_id){
					  		item.className="mui-btn selected";
					  	}
					});
					
					var arr_list3=document.querySelectorAll('#list-area .mairu_list.store_brands .mui-btn');
					mui.each(arr_list3,function(index,item){
					  	if(item.getAttribute('data-id')==res.store.brand_id){
					  		item.className="mui-btn selected";
					  	}
					});
					document.getElementById('check_1').style.display="block";
					store_id=res.store.store_id;
				}
				else{
					document.querySelectorAll('#list-area .mairu_list.store_types .mui-btn')[0].className="mui-btn selected";
		        	document.querySelectorAll('#list-area .mairu_list.store_business .mui-btn')[0].className="mui-btn selected";
			    	document.querySelectorAll('#list-area .mairu_list.store_brands .mui-btn')[0].className="mui-btn selected";
				}
				
			},
			error: function(xhr, type, errorThrown) {
				//console.log(JSON.stringify(xhr));
				//console.log(JSON.stringify(type));
				//console.log(JSON.stringify(errorThrown));
				mui.toast('操作失败！');
			}
		});
			
			
		mui("#list-area").on('tap','.mairu_list.store_types .mui-btn',function(){
			if(store_id>0){
				return ;
			}
			var arr_list=document.querySelectorAll('#list-area .mairu_list.store_types .mui-btn');
			mui.each(arr_list,function(index,item){
			  	item.className="mui-btn";
			});
            this.className="mui-btn selected";
		})

		mui("#list-area").on('tap','.mairu_list.store_business .mui-btn',function(){
			if(store_id>0){
				return ;
			}
			var arr_list=document.querySelectorAll('#list-area .mairu_list.store_business .mui-btn');
			mui.each(arr_list,function(index,item){
			  	item.className="mui-btn";
			});
            this.className="mui-btn selected";
		})
		
		mui("#list-area").on('tap','.mairu_list.store_brands .mui-btn',function(){
			if(store_id>0){
				return ;
			}
			var arr_list=document.querySelectorAll('#list-area .mairu_list.store_brands .mui-btn');
			mui.each(arr_list,function(index,item){
			  	item.className="mui-btn";
			});
            this.className="mui-btn selected";
		})
		
		/*
		mui(".shop-name").on('change','#store_name',function(){
			mui.ajax({
				url: apiurl + "Store/check_store_name/",
				data: {
					'user_id': userinfo.user_id,
					'store_name':this.value.trim()
				},
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'get',
				timeout: 10000,
				success: function(res) {
					if(res.status==1){
						document.getElementById('check_1').style.display="block";
						document.getElementById('check_0').style.display="none";
					}
					else{
						document.getElementById('check_1').style.display="none";
						document.getElementById('check_0').style.display="block";
					}
					
				},
				error: function(xhr, type, errorThrown) {
					//console.log(JSON.stringify(xhr));
					//console.log(JSON.stringify(type));
					//console.log(JSON.stringify(errorThrown));
					mui.toast('操作失败！');
				}
			});
		})*/
		
	})
	
}(mui, document));


function do_post(){
	var userinfo = app.getState() || {};
	var Info={
		user_id:document.getElementById('user_id').value,
		mobile:document.getElementById('mobile').value,
		store_name:document.getElementById('store_name').value.trim(),
		_token:userinfo.token
	};
	if(!Info.store_name){
		mui.alert('店铺名称不能为空');
		return false;
	}
	if(parseFloat(document.getElementById('a1_money').value) <1000){
		mui.alert('余额不足1000，不能申请店铺');
		return false;
	}
	Info.type_id=document.querySelector('#list-area .mairu_list.store_types .mui-btn.selected').getAttribute('data-id');
	Info.business_id=document.querySelector('#list-area .mairu_list.store_business .mui-btn.selected').getAttribute('data-id');
    Info.brand_id=document.querySelector('#list-area .mairu_list.store_brands .mui-btn.selected').getAttribute('data-id');
	mui.ajax({
				url: apiurl + "Store/check_store_name/",
				data: {
					'user_id': Info.user_id,
					'store_name':Info.store_name,
					'_token':userinfo.token
				},
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'get',
				timeout: 10000,
				success: function(res) {
					if(res.status ==-3){
						mui.toast(res.msg);
						app.out();
						return false;
					}
					if(res.status==1){
						document.getElementById('check_1').style.display="block";
						document.getElementById('check_0').style.display="none";
						mui.ajax({
							url: apiurl + "Store/apply_store/",
							data: Info,
							async: true,
							dataType: 'json',
							crossDomain: true, //强制使用5+跨域
							type: 'post',
							timeout: 10000,
							success: function(res) {
								console.log(JSON.stringify(res));
								if(res.status ==-3){
									mui.toast(res.msg);
									app.out();
									return false;
								}
								
								// 请求成功
								if(res.status==-1){
									document.getElementById('check_1').style.display="none";
									document.getElementById('check_0').style.display="block";
								}
								else{
									document.getElementById('check_1').style.display="block";
									document.getElementById('check_0').style.display="none";
								}
								
								if(res.status==0){
									mui.alert(res.msg);
									return false;
								}
								if(res.status==1){
									//document.querySelector('.mui-button-group.apply').style.display="none";
									//document.querySelector('.mui-button-group.audit').style.display="block";
								    mui.toast('申请店铺成功');
								    mui.back();
								    return;
								}
							},
							error: function(xhr, type, errorThrown) {
								//console.log(JSON.stringify(xhr));
								//console.log(JSON.stringify(type));
								//console.log(JSON.stringify(errorThrown));
								mui.toast('操作失败！');
							}
						});
					}
					else{
						document.getElementById('check_1').style.display="none";
						document.getElementById('check_0').style.display="block";
					}
					
				},
				error: function(xhr, type, errorThrown) {
					//console.log(JSON.stringify(xhr));
					//console.log(JSON.stringify(type));
					//console.log(JSON.stringify(errorThrown));
					mui.toast('操作失败！');
				}
			});
	
}
